/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.cloudfarming.client.geoviewer.layers;

import nl.cloudfarming.client.geoviewer.Category;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import nl.cloudfarming.client.geoviewer.Layer;
import nl.cloudfarming.client.geoviewer.MapController;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.nodes.NodeTransfer;
import org.openide.util.datatransfer.PasteType;
import org.openide.util.lookup.Lookups;

/**
 * The Node of a category
 * 
 * @author Maarten Hollenga
 */
public class CategoryNode extends AbstractNode {

    public CategoryNode(MapController mapApi, Category category) {
        super(new LayerChildren(mapApi, category), Lookups.fixed(mapApi, category));
        setDisplayName(category.getName());
    }

    /**
     * Handles the dragging of nodes. The old node will be deleted, and a new node be created at the
     * place to which the old node was dragged
     * 
     * @param t Information about the drag action
     * @param action The action that was performed on the node (copy, paste etc..) We ignore this, because we always move the node, instead of copying.
     * @param index Is useless? This always contains either -1 or 0, so we can't use this to retrieve the new index of the dragged node...
    
     * @return A PasteType object, if the node can be copied/cut. A null if it is now allowed.
     */
    @Override
    public PasteType getDropType(Transferable t, final int action, int index) {

        // retrieve the selected node
        final Node dropNode = NodeTransfer.node(t,
                DnDConstants.ACTION_COPY_OR_MOVE + NodeTransfer.CLIPBOARD_CUT);

        // if a node was selected, we will move it now. Additionally, we also check if the node's parentNode isn't changed (i.e. you cannot move a node to another parentnode)
        if (dropNode != null && this.equals(dropNode.getParentNode())) {

            final Layer selectedLayer = dropNode.getLookup().lookup(Layer.class);
            final MapController selectedMapApi = dropNode.getLookup().lookup(MapController.class);

            if (selectedLayer != null && selectedMapApi != null) {

                return new PasteType() {

                    public Transferable paste() {

                        // adds the new node to the selected parent (current node)
                        dropNode.getParentNode().getChildren().add(new Node[]{new LayerNode(selectedMapApi, selectedLayer)});
                        // remove the node from the old location
                        dropNode.getParentNode().getChildren().remove(new Node[]{dropNode});
                        return null;
                    }
                };
            }
        }
        // default return
        return null;
    }

    /**
     * Returns a cookie. Overridden to add support for the IndexCookie, which is needed to keep track of the position of this node.
     * 
     * @param clazz The class for which the cookie should be returned
     * 
     * @return The cookie object, or null if no cookie was found for the specified class.
     */
    @Override
    @SuppressWarnings("unchecked")
    public Cookie getCookie(Class clazz) {
        Children ch = getChildren();

        if (clazz.isInstance(ch)) {
            return (Cookie) ch;
        }

        return super.getCookie(clazz);
    }
}
